#include "DataMiningManager.h"
#include "DataMiningConst.h"
#include "DataMiningStruct.h"
#include <string>
#include <time.h>

DataMiningManager::DataMiningManager()
{
	int_x currentTime = static_cast<int>( time(NULL) );
	char fileName[32];
	memset( fileName, 0, 32);
	sprintf(fileName, "%d", currentTime );

	_currentFile = fopen( fileName, "wb");

	init();
}

DataMiningManager::~DataMiningManager()
{
	fclose( _currentFile );
}

void DataMiningManager::trace(DataMiningBase* data, int_x dataType )
{	
	int_x currentTime = static_cast<int>( time(NULL) );
	if ( currentTime > _createFileTime + 60)
	{
		_createFileTime = currentTime;
		fflush( _currentFile );
		fclose( _currentFile );
		char fileName[32];
		memset( fileName, 0, 32);
		sprintf(fileName, "%d", currentTime );
		_currentFile = fopen( fileName, "wb");

	}
	fwrite( &dataType, sizeof(dataType), 1, _currentFile );
	
	data->doTime = currentTime;
	data->dataType = dataType;
	int_x dateLen = structSizes[ dataType ];
	fwrite( data, dateLen, 1, _currentFile );
}

void DataMiningManager::init()
{
	structSizes[ DATA_MINING_TYPE_BUYDATA ] = sizeof( BuyData );
	structSizes[ DATA_MINING_TYPE_EQUIPSTRENDATA ] = sizeof( EquipStrenData );
	structSizes[ DATA_MINING_TYPE_RECHARGE ] = sizeof( RechargeData );
}